package team.kirohuji.controller;


import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import team.kirohuji.domain.Priority;
import team.kirohuji.exception.CustomException;
import team.kirohuji.repository.PriorityRepository;
import team.kirohuji.service.PriorityService;
import team.kirohuji.service.PriorityService.MenuType;
import team.kirohuji.utils.R;

/**
 * 这个类主要是目录的用户的权限的管理
 * @author kirohuji
 *
 */
@RestController
@RequestMapping("/menu")
public class PriorityController extends AbstractController {
	
	@Autowired
	private PriorityService priorityService;
	
	@Autowired
	private PriorityRepository priorityRepository;
	
	@RequestMapping(value = "/lists", method=RequestMethod.GET)
	public Page<Priority> getLists(@RequestParam(value = "page", defaultValue = "0") Integer page,
	        @RequestParam(value = "size", defaultValue = "5") Integer size) {
	    Sort sort = new Sort(Direction.DESC, "priorityId");
	    Pageable pageable = new PageRequest(page, size, sort);
	    return priorityRepository.findAll(pageable);
	}
	/**
	 * 选择菜单(添加、修改菜单)
	 */
	@RequestMapping("/select")
	/*@RequiresPermissions("sys:menu:select")*/
	public R select(){
		//查询列表数据
		List<Priority> menuList = priorityService.queryNotButtonList();
		//添加顶级菜单
		Priority root = new Priority();
		root.setPriorityId(0);
		root.setName("一级菜单");
		root.setParentId(-1);
		root.setOpen(true);
		menuList.add(root);
		return R.ok().put("menuList", menuList);
	}
	/**
	 * 角色授权菜单
	 * @param type 权限类型：0-系统后台用户权限，1-微信端用户权限
	 */
	@RequestMapping("/perms/{type}")
	/*@RequiresPermissions("sys:menu:perms")*/
	public R perms(@PathVariable("type") String type){
		if(type!=null){
			//查询列表数据
			List<Priority> menuList = priorityService.queryPriorityByPriorityType(type);
			return R.ok().put("menuList", menuList);
		}else {
			return R.error();
		}
	}
	
	/**
	 * 菜单信息
	 */
	@RequestMapping("/info/{menuId}")
	/*@RequiresPermissions("sys:menu:info")*/
	public R info(@PathVariable("menuId") Integer menuId){
		Priority menu = priorityRepository.findOne(menuId);
		return R.ok().put("menu", menu);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	/*@RequiresPermissions("sys:menu:save")*/
	public R save(@RequestBody Priority menu){
		//数据校验
		verifyForm(menu);
		
		priorityRepository.save(menu);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	/*@RequiresPermissions("sys:menu:update")*/
	public R update(@RequestBody Priority menu){
		//数据校验
		verifyForm(menu);
				
		priorityRepository.save(menu);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	/*@RequiresPermissions("sys:menu:delete")*/
	public R delete(@RequestBody Integer[] menuIds){
		for(Integer menuId : menuIds){
			if(menuId.intValue() <= 28){
				return R.error("系统菜单，不能删除");
			}
		}
		for(Integer menuId : menuIds){
			priorityRepository.delete(menuId);
		}
		return R.ok();
	}
	
	/**
	 * 用户菜单列表
	 */
	@RequestMapping("/user")
	public R user(){
		List<Priority> menuList = priorityService.getUserMenuList(getUserId(),0);
		System.out.println(menuList);
		return R.ok().put("menuList", menuList);
	}
	
	/**
	 * 验证参数是否正确
	 */
	private void verifyForm(Priority menu){
		if(StringUtils.isBlank(menu.getName())){
			throw new CustomException("菜单名称不能为空");
		}
		
		if(menu.getParentId() == null){
			throw new CustomException("上级菜单不能为空");
		}
		
		//菜单
		if(menu.getNodeType() == MenuType.MENU.getValue()){
			if(StringUtils.isBlank(menu.getUrl())){
				throw new CustomException("菜单URL不能为空");
			}
		}
		
		//上级菜单类型
		int parentType = MenuType.CATALOG.getValue();
		if(menu.getParentId() != 0){
			Priority parentMenu = priorityRepository.findOne(menu.getParentId());
			parentType = parentMenu.getNodeType();
		}
		
		//目录、菜单
		if(menu.getNodeType() == MenuType.CATALOG.getValue() ||
				menu.getNodeType() == MenuType.MENU.getValue()){
			if(parentType != MenuType.CATALOG.getValue()){
				throw new CustomException("上级菜单只能为目录类型");
			}
			return ;
		}
		
		//按钮
		if(menu.getNodeType() == MenuType.BUTTON.getValue()){
			if(parentType != MenuType.MENU.getValue()){
				throw new CustomException("上级菜单只能为菜单类型");
			}
			return ;
		}
	}
}
